package service

import (
	"fmt"
	ClickHouse "gin/database"
	"gin/model"
)

type ReviewRequest struct {
}

type ReviewRecord struct {
	VersionId        uint64 `json:"-"`
	LinkId           uint64 `json:"-"`
	ModuleId         uint64 `json:"-"`
	ProjectId        uint64 `json:"-"`
	ProjectName      string `json:"-"`
	ReviewRequestId  uint64 `json:"-"`
	ReviewIsReviewed string `json:"-"`
	ReviewIsPass     string `json:"-"`
	ReviewReviewed   uint64 `json:"-"`

	Name        string `json:"name" gorm:"column:project_name"`
	IsPass      bool   `json:"is_pass"`
	Time        uint64 `json:"time" gorm:"column:review_reviewed"`
	VersionText string `json:"version_text" gorm:"column:version_number"`
}

type ReviewRecordTaskInfo struct {
	Id               uint64 `json:"id"`
	EntityId         uint64 `json:"entity_id"`
	Name             string `json:"name"`
	Code             string `json:"code"`
	StepCategoryId   uint64 `json:"step_category_id"`
	StepId           uint64 `json:"step_id"`
	StepCategoryName string `json:"step_category_name"`
	EntityName       string `json:"entity_name"`
}
type ReviewRecordRes struct {
	Total int64          `json:"total"`
	Rows  []ReviewRecord `json:"rows"`
}

// ReviewRecord 审核记录
func (t ReviewRequest) ReviewRecord(tenantId uint64, page []int) (res ReviewRecordRes, err error) {
	moduleModel := model.Module{}
	versionModule, err := moduleModel.GetByCode("version")
	if err != nil {
		return
	}
	taskModule, err := moduleModel.GetByCode("task")
	if err != nil {
		return
	}
	// 审核记录统计
	err = ClickHouse.DB.Model(&model.Version{}).
		Joins("join review_request on review_request.link_id = version.id and review_request.tenant_id = version.tenant_id").
		Where("review_request.module_id = ? and review_request.tenant_id = ? and review_request.is_reviewed = 'yes' and version.module_id = ?", versionModule.Id, tenantId, taskModule.Id).
		Count(&res.Total).Error
	if err != nil {
		return
	}

	err = ClickHouse.DB.Model(&model.Version{}).
		Joins("join review_request on review_request.link_id = version.id and review_request.tenant_id = version.tenant_id join project on project.id = version.project_id").
		Where("review_request.module_id = ? and review_request.is_reviewed = 'yes' and version.module_id = ? and version.tenant_id = ? and review_request.tenant_id = ?", versionModule.Id, taskModule.Id, tenantId, tenantId).
		Select("version.id AS version_id,version.link_id AS link_id,version.module_id AS module_id,version.number as version_number,project.id AS project_id,project.name AS project_name,review_request.id AS review_request_id,review_request.is_reviewed AS review_is_reviewed,review_request.is_pass AS review_is_pass,review_request.reviewed AS review_reviewed").
		Offset((page[0] - 1) * page[1]).
		Limit(page[1]).
		Order("review_request.reviewed DESC").
		Find(&res.Rows).Error
	if err != nil {
		return
	}
	if len(res.Rows) == 0 {
		return
	}

	// 提取任务id
	var taskIdMap = make(map[uint64]uint64)
	for _, row := range res.Rows {
		taskIdMap[row.LinkId] = row.LinkId
	}
	var taskIdList = make([]uint64, 0, len(taskIdMap))
	for taskId := range taskIdMap {
		taskIdList = append(taskIdList, taskId)
	}

	var taskList = make([]ReviewRecordTaskInfo, len(taskIdList))
	err = ClickHouse.DB.Model(&model.StepCategory{}).
		Joins("join task on task.step_category_id = step_category.id join entity on task.entity_id = entity.id").
		Where("task.id in ?", taskIdList).
		Select("task.id as id,task.entity_id as entity_id,task.name as name,task.code as code,task.step_category_id as step_category_id,task.step_id as step_id,step_category.name as step_category_name,entity.name as entity_name").
		Find(&taskList).Error
	if err != nil {
		return
	}
	var taskMap = make(map[uint64]*ReviewRecordTaskInfo)
	for _, info := range taskList {
		taskMap[info.Id] = &info
	}
	for i, row := range res.Rows {
		res.Rows[i].Name = res.Rows[i].ProjectName
		res.Rows[i].Time = res.Rows[i].ReviewReviewed
		res.Rows[i].IsPass = res.Rows[i].ReviewIsPass == "yes"
		if _, ok := taskMap[row.LinkId]; ok {
			if taskMap[row.LinkId].StepId > 0 {
				res.Rows[i].Name += fmt.Sprintf(" %s %s %s", taskMap[row.LinkId].EntityName, taskMap[row.LinkId].StepCategoryName, taskMap[row.LinkId].Name)
			} else {
				res.Rows[i].Name += fmt.Sprintf(" %s %s", taskMap[row.LinkId].EntityName, taskMap[row.LinkId].Name)
			}
		}
	}

	return
}
